
package nu.aing.P20140602;

import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class CandidatesSelectionEasy {

    public static void main(String[] args) {
        
        CandidatesSelectionEasy instance = new CandidatesSelectionEasy();
        TestUtils.test(instance, "sort", new String[] { "ACB", "BAC", "CBA" }, 1 );
        TestUtils.test(instance, "sort", new String[] { "ACB", "BAC", "CBA" }, 1);
        TestUtils.test(instance, "sort", new String[] { "A", "C", "B", "C", "A" }, 0);
        TestUtils.test(instance, "sort", new String[] { "LAX", "BUR", "ONT", "LGB", "SAN", "SNA", "SFO", "OAK", "SJC" }, 2);
        TestUtils.test(instance, "sort", new String[] { "BBCBABAC", "BCBACABA", "CCCBAACB", "CACABABB", "AABBBBCC" }, 6);
        TestUtils.test(instance, "sort", new String[] { "XXYWZWWYXZ", "YZZZYWYZYW", "ZYZZWZYYWW", "ZWZWZWZXYW", "ZYXWZXWYXY", "YXXXZWXWXW",
                "XWWYZWXYXY", "XYYXYWYXWY", "ZZYXZYZXYY", "WXZXWYZWYY" }, 3);
        TestUtils.test(instance, "sort", new String[] { "X" }, 0);
    }

    public final int[] sort(String[] score, int x) {
        SortedMap<Character, List<Integer>> maps = new TreeMap<Character, List<Integer>>();
        for (int i = 0; i < score.length; i++) {
            if (!maps.containsKey(score[i].charAt(x))) {
                maps.put(score[i].charAt(x), new LinkedList<Integer>());
            }
            maps.get(score[i].charAt(x)).add(i);
        }

        int y = 0;
        int[] result = new int[score.length];
        for (List<Integer> indexes : maps.values()) {
            for (int i = 0; i < indexes.size(); i++) {
                result[y++] = indexes.get(i);
            }
        }

        return result;
    }
    
}
